local xiangle = fk.CreateSkill {

  name = "joyex__xiangle",

  tags = { Skill.Compulsory, },

}



xiangle:addEffect(fk.TargetConfirmed, {
  name = "joyex__xiangle",
  events = {fk.TargetConfirmed},
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xiangle.name) and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if #room:askForDiscard(room:getPlayerById(data.from), 1, 1, false, xiangle.name, true, ".|.|.|.|.|basic", "#xiangle-discard:"..player.id) == 0 then
      table.insertIfNeed(data.nullifiedTargets, player.id)
    end
  end,
})

xiangle:addEffect(fk.RoundEnd, {
  name = "#joyex__xiangle_delay",
  events = {fk.RoundEnd},
  anim_type = "support",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(xiangle.name) then
      local events = player.room.logic:getEventsOfScope(GameEvent.UseCard,1, function(e)
        local use = e.data[1]
        return use.from == player.id
      end, Player.HistoryRound)
      return #events == 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local n = player.seat//2
    local cards = player.room:askForCard(player,n,n,true,xiangle.name,true,".","享乐：你可以弃置"..n.."张牌并翻面，然后发动一次〖放权〗的效果。")
    if #cards == n then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if #event:getCostData(self) > 0 then
      room:throwCard(event:getCostData(self),xiangle.name,player,player)
    end
    if player.dead then return end
    player:turnOver()
    if player.dead then return end
    local tar, cards = {},{}
    tar = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player), Util.IdMapper),1,1, "放权：选择一名其他角色，然后交给其任意张牌并令其获得一个额外的回合。","joyex__fangquan",true)
       if #tar > 0  then
      local to =  room:getPlayerById(tar[1])
      cards = room:askForCard(player,1,#player:getCardIds("he"),true,"joyex__fangquan",true,".","你可以交给其任意张牌。")
      if #cards > 0 then
        room:moveCardTo(cards, Card.PlayerHand,to,fk.ReasonGive,"joyex__fangquan", nil,false, player.id)
      end
      if not to.dead then
        to:gainAnExtraTurn()
      end
    end
  end,
})

return xiangle